//
//  CZPictureView.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/3/5.
//  Copyright © 2016年 张雪雪. All rights reserved.
//



import UIKit
import SDWebImage

//定义(相当于宏)常量的时候,最好把他定义在类外边
//定义一个间距
let ItemMargin:CGFloat = 10
let pictureMargin: CGFloat = 5
//定义一个ItemWH
let ItemWH = (kUIScreenWidth - 4 * ItemMargin)/3

//定义一个cell重用表示
let CZPictureViewCollectionViewReuserIdentifer = "CZPictureViewCollectionViewReuserIdentifer"




class CZPictureView: UICollectionView , UICollectionViewDataSource{
    
    
//    var pictureViewModel:CZStatusViewModel?{
//    
//    
//        didSet{
//        
//        //更新配图
//            self.snp_updateConstraints { (make) -> Void in
//                
//                
//                let size = self.calPictureSize()
//                make.width.equalTo(size.width)
//                make.height.equalTo(size.height)
//             }
//            
//            
//            
//        //赋值以后需要更新数据
//            reloadData()
//        }
//    
//    
//    }
    
    var pictureList:[CZPictureViewModel]?{
    
        didSet{
        
        //更新配图约束
            self.snp_updateConstraints { (make) -> Void in
                
                let size = self.calPictureSize()
                 make.width.equalTo(size.width)
                make.height.equalTo(size.height)
                
            }
        
            addSubview(label)
            //让测试label居中显示
            label.snp_makeConstraints { (make) -> Void in
                make.center.equalTo(self.snp_center)
            }
        
            label.text = "\(pictureList?.count ?? 0)"
        
        //赋值以后,是不是需要重新下加载数据
            reloadData()
            
            
        }
    
    }

    
    
    
    //传递数据
    var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
   
        //设置流水布局
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//设置控件
    private func setupUI(){
        
        //backgroundColor = UIColor.whiteColor()
        /**
        实现数据源方法
        设置布局以及UICollectionView的重复属性
        
        显示cell -- 自定义cell
        */
    
    self.dataSource = self
        
      //注册cell
        self.registerClass(CZPictureViewCollectionCell.self, forCellWithReuseIdentifier:CZPictureViewCollectionViewReuserIdentifer)
        
        //设置水平
       // flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal

        // 设置cell的大小
        
        flowLayout.itemSize = CGSizeMake(ItemWH - 1, ItemWH - 1)
        flowLayout.minimumInteritemSpacing = ItemMargin
        
        flowLayout.minimumLineSpacing = ItemMargin

    }

    override func layoutSubviews() {
        super.layoutSubviews()
    }

// MARK:  -- 计算视图的宽度
    private func calPictureSize() -> CGSize{
    
    //需要图片的个数
        let count = pictureList?.count//pictureViewModel?.statusModel?.pic_urls?.count
    
        if count <= 3{
            //宽 = itemW * 3 + 2 * itemMargin ,
            //高 itemH
        return CGSizeMake(ItemWH * 3 + 2 * ItemMargin,ItemWH)
            
        
        }else if count == 4 {
        
            //4张图片, 宽 = itmeW * 2 + itemMargin 高 = 宽
            return CGSizeMake(ItemWH * 2 + ItemMargin, ItemWH * 2 + ItemMargin)

        }else {
        
        //注意5,6,7,8,9九宫格
            
            let row = CGFloat((count! - 1)/3 + 1)
            
            return CGSizeMake(kUIScreenWidth - 2 * ItemMargin, (ItemWH + ItemMargin) * row)
 
        }
        
    }
   
    //添加一个测试label
    private lazy var label: UILabel = {
        
        let label = UILabel()
        
        return label
    }()

}
//代码模块化
//MARK:--实现数据源方法
extension CZPictureView{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureList?.count ?? 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZPictureViewCollectionViewReuserIdentifer, forIndexPath: indexPath) as! CZPictureViewCollectionCell
        
        //调试方便，随机颜色
        cell.backgroundColor = randomColor()
       // let viewModel = CZPictureViewModel()
        
        //viewModel.model = pictureViewModel?.statusModel?.pic_urls![indexPath.item]
        
        cell.pictureViewModel = pictureList![indexPath.item]
        
        return cell
    }

}

class CZPictureViewCollectionCell:UICollectionViewCell {
    
    //数据传值MVVM
    var pictureViewModel:CZPictureViewModel?{
    
        
        
        didSet{
        
        
            let url = NSURL(string: pictureViewModel?.model?.thumbnail_pic ?? "")
            
            if let Ourl = url {
                
                iconImageView.sd_setImageWithURL(Ourl)
                
                
            }
            
        }
        
        
   }
    //三部曲
    override init(frame:CGRect){
        
        super.init(frame:frame)
        
        setupUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("inin(coder) has not been implemented")
    }
    
    func setupUI(){
    
    addSubview(iconImageView)
    
    iconImageView.frame = self.bounds
    
    }
    
    //懒加载
    private lazy var iconImageView:UIImageView = {
    
    let img = UIImageView()
        
        return img
    
    }()
    
    
    
}

